<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Constants\ErrorCode;
use App\Controller\AbstractController;
use App\Service\OilChannelService;
use App\Validate\OilChannelValidation;
use Hyperf\Di\Annotation\Inject;
use App\Utils\ApiUtils;

class OilChannelController extends AbstractController
{

    /**
     * @Inject
     * @var OilChannelService
     */
    protected $oilChannelService;

    /**
     * 获取合作商列表
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function list()
    {
        $page = (int)$this->request->input('page', 1);
        $pageSize = (int)$this->request->input('pageSize', 15);
        $name = $this->request->input('name', '');
        $result = $this->oilChannelService->list($page, $pageSize, $name);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$result['list'],$result['count']));
    }

    /**
     * 获取全部合作商
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function all()
    {
        $result = $this->oilChannelService->all();
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$result));
    }


    /**
     * 添加合作商
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function add()
    {
        //接收参数
        $data['name'] = $this->request->input('name');
        $data['channel_sn'] = $this->request->input('channel_sn');
        $data['api_info'] = $this->request->input('api_info');
        //$data['external_channel_id'] = $this->request->input('external_channel_id');
        $data['merchant_no'] = $this->request->input('merchant_no');
        $data['api_name'] = $this->request->input('api_name');
        $data['appkey'] = $this->request->input('appkey');
        $data['status'] = $this->request->input('status');
        $data['start_time'] = $this->request->input('start_time');
        $data['sort'] = $this->request->input('sort', 1);
        $data['type'] = $this->request->input('type');
        make(OilChannelValidation::class)->check($data,'add')->response();
        $result = $this->oilChannelService->add($data);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'添加成功',$result));
    }

    /**
     * 编辑合作商
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function edit()
    {

        $id = $this->request->input('id');
        if($this->request->isMethod('post')){
            //接收参数
            $data['id'] = $id;
            $data['name'] = $this->request->input('name');
            $data['channel_sn'] = $this->request->input('channel_sn');
            $data['api_info'] = $this->request->input('api_info');
            //$data['external_channel_id'] = $this->request->input('external_channel_id');
            $data['merchant_no'] = $this->request->input('merchant_no');
            $data['api_name'] = $this->request->input('api_name');
            $data['appkey'] = $this->request->input('appkey');
            $data['status'] = $this->request->input('status');
            $data['start_time'] = $this->request->input('start_time');
            $data['sort'] = $this->request->input('sort', 1);
            $data['type'] = $this->request->input('type');
            make(OilChannelValidation::class)->check($data,'edit_post')->response();

            $this->oilChannelService->edit($data);
            return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'修改成功'));

        }
        make(OilChannelValidation::class)->check(['id'=>$id],'edit_get')->response();
        $result = $this->oilChannelService->get($id);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$result));


    }


    /**
     * 删除合作商
     *
     */
    public function del()
    {
        $id = $this->request->input('id');
        make(OilChannelValidation::class)->check(['id'=>$id],'del')->response();
        $this->oilChannelService->del($id);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'删除成功'));
    }

    /**
     * 编辑合作商备注信息
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function remark()
    {
        $id = $this->request->input('id');
        $remark = $this->request->input('remark');
        make(OilChannelValidation::class)->check(['id'=>$id, 'remark'=>$remark],'remark_post')->response();
        $this->oilChannelService->remark($id, $remark);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'修改成功'));
    }


    /**
     * 编辑合作商充值金额
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function rechargeAmount()
    {
        $id = $this->request->input('id');
        if($this->request->isMethod('post')){
            //接收参数
            $rechargeAmount = $this->request->input('recharge_amount');
            make(OilChannelValidation::class)->check(['id'=>$id, 'recharge_amount'=>$rechargeAmount],'rechargeAmount_post')->response();
            $this->oilChannelService->rechargeAmountEdit($id, $rechargeAmount);
            return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'修改成功'));

        }
        make(OilChannelValidation::class)->check(['id'=>$id],'rechargeAmount_get')->response();
        $result = $this->oilChannelService->rechargeAmount($id);
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'获取成功',$result));
    }







   
}
